/*********固码*********/
define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/client/payAmount.html",
    "wTools",
    "area",
    "validate",
    "antbridge"

], function ($, _, Backbone, template, antbridge) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        events: {
            "tap #btn": "btn",
            "input #amount":"clearNoNum"
        },
        initialize: function () {
            $("head").append('<META HTTP-EQUIV="pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"><META HTTP-EQUIV="expires" CONTENT="0">');
            $("#head_back").hide();
            //alert(window.navigator.userAgent)
            var postData = {
                'memberId': wData.urlData.memberId,
                'subMemberId':wData.urlData.subMemberId
            }, that = this;
           
            
            wAjaxForm("getMerInfo", "", postData, function (result) {
                if (result.success) {
                    wData.childName = result.data.childName;
                    wData.QPAY=result.data.QPAY;
                    wData.WXPay=result.data.WXPay;
                    that.render(wData);
                }else{
                    wTips("请重试");
                }
            });
        },
        /*********2017/10/12 张迪  添加 通过JSAPI在付款页面唤起收银台 方法 start*********/
        payFormAccount: function(callback) {
            // 如果jsbridge已经注入则直接调用
            if (window.AlipayJSBridge) {
                callback && callback();
            } else {
                // 如果没有注入则监听注入的事件
                document.addEventListener('AlipayJSBridgeReady', callback, false);
            }
        },
        /*********2017/10/12 张迪  添加 通过JSAPI在付款页面唤起收银台 方法 end  *********/
        back: function () {
            history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        btn: function () {
           
            if ($("#amount").val() != "") {
                var postData = {
                    'token':wData.urlData.token,
                    'amount':$('#amount').val()
                };
                /*********2017/10/17 张迪  修改唤起支付宝付款方式 start  *********/
                if (wIsAlipay()) {
                    
                    var postData = {
                        "amount": $("#amount").val(),
                        'token':wData.urlData.token,
                    };
                    wAjaxForm("oneCodeRandomPay", "", postData, function (result) {
                        if (result.success) {
                            location.href = result.data.codeUrl;


                            // this.payFormAccount(function(){
                            //     AlipayJSBridge.call("tradePay",{tradeNO: "2017112721001004385328030746"}, function(result){
                            //     alert(JSON.stringify(result));
                            //     location.href = 'http://www.baidu.com';
                            //     });
                            // });


                        }
                        else {
                            wTips(result.message)
                        }
                    });
                }
                /*********2017/10/17 张迪  修改唤起支付宝付款方式 end    *********/
                else if (wIsWeixin()) {
                    //微信中,直接走微信渠道
                    var postData = {
                        "amount": $("#amount").val(),
                        'token':wData.urlData.token,
                    };
                    wAjaxForm("oneCodeRandomPay", "", postData, function (result) {
                        if (result.success) {
                            location.href = result.data.codeUrl;
                        }
                        else {
                            wTips(result.message)
                        }
                    });
                }
                /************2017/12/15 添加银联****************/
                else if (wIsUnionPay()) {
                    alert('银联')
                    var postData = {
                        "amount": $("#amount").val(),
                        'token':wData.urlData.token,
                    };
                    wAjaxForm("oneCodeRandomPay", "", postData, function (result) {
                        if (result.success) {
                            location.href = result.data.codeUrl;
                        }
                        else {
                            wTips(result.message)
                        }
                    });
                }
                else {
                    //非微信浏览器,走畅捷支付渠道
                    if (wData.QPAY && wData.QPAY == "true") {
                        $("#formAmount").val($("#amount").val());
                        $("#formChanjet").attr("action", App.severUrl + App.urlList["recvOrder"]).submit();
                        wLoading.start();
                    } else {
                        wTips("尚未开通畅捷支付权限");
                    }
                }
            } else {
                wTips("请输入金额");
            }
        },
        clearNoNum: function(event) {
           var obj = $('#'+event.target.id);
           //console.log(obj.val());
            var val =obj.val();
            val = val.replace(/,/,'');
            if(val.charAt(0) == '.'){
                obj.val('0' + val);
            }
            val = obj.val();
            var regStrs = [  
                ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0
                ['^0\\d*(.\\d*)$', '0$1'], //禁止录入整数部分两位以上，但首位为0  
                ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点  
                ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点
                ['^([1-9]{1}\\d{6})(\\d+)', '$1'],
                ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上
            ];  
            for(i=0; i<regStrs.length; i++){  
                var reg = new RegExp(regStrs[i][0]);  
                val = val.replace(reg, regStrs[i][1]);
                obj.val(val);
            }
        },

        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            delete this;
        }
    });
});